package com.itheima.article.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.itheima.article.pojo.ApArticleConfig;
import com.itheima.article.service.ApArticleConfigService;
import com.itheima.common.constants.BusinessConstants;
import com.itheima.wemedia.dto.DownOrUpDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * @version 1.0
 * @description 说明
 * @package com.itheima.article.listener
 */
@Component
@Slf4j
public class ApArticleConfigListener {

    @Autowired
    private ApArticleConfigService apArticleConfigService;

    @KafkaListener(topics = BusinessConstants.MqConstants.WM_NEWS_DOWN_OR_UP_TOPIC)
    public void upOrDown(ConsumerRecord<String,String> msgRecord){
        String jsonString = msgRecord.value();
        log.info("文章微服同步上下架-开始: t={},p={},o={},v={}", msgRecord.topic(), msgRecord.partition(), msgRecord.offset(), jsonString);
        //1. 解析消息内容
        DownOrUpDto upOrDown = JSON.parseObject(jsonString, DownOrUpDto.class);
        apArticleConfigService.upOrDown(upOrDown);
        //2. 调用service做上下架处理
        log.info("文章微服同步上下架-完成: t={},p={},o={},v={}", msgRecord.topic(), msgRecord.partition(), msgRecord.offset(), jsonString);
    }
}
